package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.TravelLike;
import cn.wolfcode.trip.base.mapper.TravelLikeMapper;
import cn.wolfcode.trip.base.service.ITravelLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class TravelLikeServiceImpl implements ITravelLikeService {
    @Autowired
    private TravelLikeMapper travelLikeMapper;

    @Override
    public Map<String, Object> checkNumByUserIdAndStrategyId(Long userId, Long travelId) {
        Map<String, Object> map = new HashMap<>();
        // 根据userId和travelId查询数据库是否有记录
        TravelLike travelLike = travelLikeMapper.queryExistLikeByUserIdAndTravelId(userId, travelId);
        if (travelLike == null) {
            // 1保存一条记录
            TravelLike like = new TravelLike();
            like.setUserId(userId);
            like.setTravelId(travelId);
            like.setTime(new Date());
            travelLikeMapper.insert(like);
            // 2点赞数记录表保存一条数据
            travelLikeMapper.insertTravelNum(userId, travelId, 1L);
            // 3查询当前攻略的点赞数
            map.put("state", 0);
            map.put("num", travelLikeMapper.queryNumByTravelId(travelId));
            return map;
        } else {
            // 删除一条记录
            travelLikeMapper.delete(userId);
            // 2点赞数记录表删除一条数据
            travelLikeMapper.deleteTravelNum(userId, travelId);
            // 3查询当前攻略的点赞数
            map.put("state", 1);
            map.put("num", travelLikeMapper.queryNumByTravelId(travelId));
            return map;
        }
    }

    @Override
    public int checkIsLikeByUserIdAndTravelId(Long userId, Long travelId) {
        TravelLike travelLike = travelLikeMapper.queryExistLikeByUserIdAndTravelId(userId, travelId);
        if (travelLike==null){
            return 0;
        }else{
            return 1;
        }
    }
}
